package com.graduation.smartparkingplatform.Utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.util.StringUtils;

import java.util.Properties;

@Slf4j
public class RedisKeyChangeListener implements MessageListener {

  @Autowired RedisTemplate redisTemplate;

  private final String listenerKeyName; // 监听的key的名称

  private static final Topic TOPIC_KEYNAMESPACE_NAME_Railing_in =
      new PatternTopic("__keyspace@0__:Railing_in");
  private static final Topic TOPIC_KEYNAMESPACE_NAME_Railing_out =
      new PatternTopic("__keyspace@0__:Railing_out");
  // 监控
  private String keyspaceNotificationsConfigParameter = "KEA";

  public RedisKeyChangeListener(
      RedisMessageListenerContainer listenerContainer, String listenerKeyName) {
    this.listenerKeyName = listenerKeyName;
    initAndSetRedisConfig(listenerContainer);
  }

  public void initAndSetRedisConfig(RedisMessageListenerContainer listenerContainer) {

    if (StringUtils.hasText(keyspaceNotificationsConfigParameter)) {

      RedisConnection connection = listenerContainer.getConnectionFactory().getConnection();

      try {

        Properties config = connection.getConfig("notify-keyspace-events");

        if (!StringUtils.hasText(config.getProperty("notify-keyspace-events"))) {
          connection.setConfig("notify-keyspace-events", keyspaceNotificationsConfigParameter);
        }

      } finally {
        connection.close();
      }
    }
    // 注册消息监听1
    listenerContainer.addMessageListener(this, TOPIC_KEYNAMESPACE_NAME_Railing_in);
    // 注册消息监听2
    listenerContainer.addMessageListener(this, TOPIC_KEYNAMESPACE_NAME_Railing_out);
  }

  @Override
  public void onMessage(Message message, byte[] pattern) {
    String Railing_in = (String) redisTemplate.opsForValue().get("Railing_in");
    String Railing_out = (String) redisTemplate.opsForValue().get("Railing_out");
    if (Railing_in.equals("car_open")) {
      new Thread() {
        public void run() {
          try {
            Thread.sleep(10000);
            redisTemplate.opsForValue().set("Railing_in", "car_close");
            log.info("进场自动关闸");
          } catch (InterruptedException e) {
            log.error("Railing_in auto close error-" + e);
          }
        }
      }.start();
    }
    if (Railing_out.equals("car_open")) {
      new Thread() {
        public void run() {
          try {
            Thread.sleep(10000);
            redisTemplate.opsForValue().set("Railing_out", "car_close");
            log.info("出场自动关闸");
          } catch (InterruptedException e) {
            log.error("Railing_out auto close error-" + e);
          }
        }
      }.start();
    }
  }
}
